import tkinter as tk
from tkinter import messagebox
from tkinter import ttk

class TodoApp:
    def __init__(self, root):
        self.root = root
        self.root.title("待办事项管理程序")

        # 初始化任务列表
        self.todo_list = []

        # 标签和文本框
        tk.Label(root, text="任务标题:").grid(row=0, column=0, sticky="w")
        self.title_entry = tk.Entry(root, width=50)
        self.title_entry.grid(row=0, column=1, columnspan=2)

        tk.Label(root, text="描述:").grid(row=1, column=0, sticky="w")
        self.description_entry = tk.Entry(root, width=50)
        self.description_entry.grid(row=1, column=1, columnspan=2)

        tk.Label(root, text="截止日期:").grid(row=2, column=0, sticky="w")
        self.deadline_entry = tk.Entry(root, width=50)
        self.deadline_entry.grid(row=2, column=1, columnspan=2)

        tk.Label(root, text="优先级:").grid(row=3, column=0, sticky="w")
        self.priority_entry = tk.Entry(root, width=50)
        self.priority_entry.grid(row=3, column=1, columnspan=2)

        # 添加任务按钮
        self.add_button = tk.Button(root, text="添加任务", command=self.add_task)
        self.add_button.grid(row=4, column=1, pady=10)

        # 待办事项列表
        self.todo_listbox = tk.Listbox(root, width=70)
        self.todo_listbox.grid(row=5, column=0, columnspan=3, padx=10, pady=10)
        self.todo_listbox.bind('<<ListboxSelect>>', self.on_select)

        # 排序选项
        self.sort_label = tk.Label(root, text="排序方式:")
        self.sort_label.grid(row=6, column=0, sticky="w")

        self.sort_choices = ttk.Combobox(root, values=["默认", "按截止日期", "按优先级"])
        self.sort_choices.current(0)  # 默认选择第一个
        self.sort_choices.grid(row=6, column=1)

        self.sort_button = tk.Button(root, text="排序", command=self.sort_tasks)
        self.sort_button.grid(row=6, column=2)

        # 编辑按钮
        self.edit_button = tk.Button(root, text="编辑任务", command=self.edit_task)
        self.edit_button.grid(row=7, column=1, pady=10)

    def add_task(self):
        title = self.title_entry.get()
        description = self.description_entry.get()
        deadline = self.deadline_entry.get()
        priority = self.priority_entry.get()

        if title and description and deadline and priority:
            new_task = {
                "标题": title,
                "描述": description,
                "截止日期": deadline,
                "优先级": priority,
                "状态": "未完成"
            }
            self.todo_list.append(new_task)
            self.update_todo_listbox()
            self.clear_entries()
        else:
            messagebox.showwarning("警告", "请填写所有字段！")

    def update_todo_listbox(self):
        self.todo_listbox.delete(0, tk.END)
        for task in self.todo_list:
            task_info = f"标题: {task['标题']}, 描述: {task['描述']}, 截止日期: {task['截止日期']}, 优先级: {task['优先级']}, 状态: {task['状态']}"
            self.todo_listbox.insert(tk.END, task_info)

    def clear_entries(self):
        self.title_entry.delete(0, tk.END)
        self.description_entry.delete(0, tk.END)
        self.deadline_entry.delete(0, tk.END)
        self.priority_entry.delete(0, tk.END)

    def sort_tasks(self):
        sort_method = self.sort_choices.get()
        if sort_method == "按截止日期":
            self.todo_list.sort(key=lambda x: x["截止日期"])
        elif sort_method == "按优先级":
            self.todo_list.sort(key=lambda x: x["优先级"])

        self.update_todo_listbox()

    def on_select(self, event):
        try:
            index = self.todo_listbox.curselection()[0]
            selected_task = self.todo_list[index]
            self.title_entry.delete(0, tk.END)
            self.title_entry.insert(tk.END, selected_task["标题"])
            self.description_entry.delete(0, tk.END)
            self.description_entry.insert(tk.END, selected_task["描述"])
            self.deadline_entry.delete(0, tk.END)
            self.deadline_entry.insert(tk.END, selected_task["截止日期"])
            self.priority_entry.delete(0, tk.END)
            self.priority_entry.insert(tk.END, selected_task["优先级"])
        except IndexError:
            pass

    def edit_task(self):
        try:
            index = self.todo_listbox.curselection()[0]
            edited_task = {
                "标题": self.title_entry.get(),
                "描述": self.description_entry.get(),
                "截止日期": self.deadline_entry.get(),
                "优先级": self.priority_entry.get(),
                "状态": self.todo_list[index]["状态"]
            }
            self.todo_list[index] = edited_task
            self.update_todo_listbox()
            self.clear_entries()
        except IndexError:
            messagebox.showwarning("警告", "请选择要编辑的任务！")

if __name__ == "__main__":
    root = tk.Tk()
    app = TodoApp(root)
    root.mainloop()

